<template>
	<div>
		<el-dialog :model-value="isDialogOpen" title="质检结果录入" width="95%" top="5vh" @close="handleClose">
			<el-form :model="schemeTaskForm" label-width="100px" class="demo-ruleForm">
				<el-row :gutter="10">
					<el-col :span="6">
						<el-form-item required label="抽样编号">
							<el-input v-model="schemeTaskForm.number" style="width:300px" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<el-table :data="configTable" height="300px" border style="width: 100%">
				<!-- 可提取公共方法生成列，减少代码量 -->
				<template v-for="col in tableColumns" :key="col.prop">
					<el-table-column :prop="col.prop" :label="col.label" :align="col.align" :min-width="col.minWidth"
													 show-overflow-tooltip />
				</template>
				<!-- 操作列 -->
				<el-table-column label="实际测量值" min-width="150" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-input v-model="scope.row.realityNumber" placeholder="实际测量值" type="number"
											style="width: 120px" />
					</template>
				</el-table-column>
				<el-table-column label="是否合格" min-width="150" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-select v-model="scope.row.isQualified" placeholder="是否合格" clearable style="width: 120px">
							<el-option label="合格" :value="true" />
							<el-option label="不合格" :value="false" />
						</el-select>
					</template>
				</el-table-column>
				<el-table-column label="不良原因" min-width="200" fixed="right" show-overflow-tooltip>
					<template #default="scope">
						<el-input v-model="scope.row.qualifiedMark" placeholder="请输入不良原因" style="width: 170px" />
					</template>
				</el-table-column>
			</el-table>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="handleClose">关闭</el-button>
					<el-button type="primary" @click="handleSaveConfig">保存</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { defineEmits, defineProps, onMounted, reactive, ref } from 'vue';

const props = defineProps({
	open: Boolean,
	rowInfo: Object
});
const isDialogOpen = ref(false);
const schemeTaskForm = reactive({ number: 0 });
const configTable = ref([]);
const tableColumns = ref([
	{ prop: 'testitemName', label: '质检项名称', align: 'center', minWidth: 180 },
	{ prop: 'testitemType', label: '质检项分类', align: 'center', minWidth: 180 },
	{ prop: 'dataType', label: '数值类型', align: 'center', minWidth: 180 },
	{ prop: 'judgmentCriteria', label: '检验标准录入方式', align: 'center', minWidth: 180 },
	{ prop: 'otherDefect', label: '标准值', align: 'center', minWidth: 180 },
	{ prop: 'upperValue', label: '上公差', align: 'center', minWidth: 180 },
	{ prop: 'lowerValue', label: '下公差', align: 'center', minWidth: 180 },
	{ prop: 'lowerBias', label: '下警戒值', align: 'center', minWidth: 180 },
	{ prop: 'upperBias', label: '上警戒值', align: 'center', minWidth: 180 },
	{ prop: 'excelCoordinate', label: 'SPC坐标', align: 'center', minWidth: 180 },
	{ prop: 'defaultValue', label: '检验默认值', align: 'center', minWidth: 180 },
	{ prop: 'spcInspection', label: 'SPC检', align: 'center', minWidth: 180 },
	{ prop: 'fullSizeInspection', label: '全尺寸检', align: 'center', minWidth: 180 },
	{ prop: 'fullSizeCoordinate', label: '全尺寸坐标', align: 'center', minWidth: 180 },
	{ prop: 'toolCoordinate', label: '量具坐标', align: 'center', minWidth: 180 },
	{ prop: 'failNameCode', label: '文件名称代码', align: 'center', minWidth: 180 },
	{ prop: 'defectMethod', label: '不良判定方式', align: 'center', minWidth: 180 },
	{ prop: 'badReasons', label: '不良原因', align: 'center', minWidth: 180 },
	{ prop: 'memo', label: '备注', align: 'center', minWidth: 180 },
	{ prop: 'createTime', label: '创建时间', align: 'center', minWidth: 180 }
]);

onMounted(() => {
	isDialogOpen.value = true;
	configTable.value = props.rowInfo.configTable;
	console.log(props.rowInfo.configTable);

});


const closeDialog = () => {
	isDialogOpen.value = false;

};

const handleClose = () => {
	emit('close');
};

function validateForm() {
	if (!schemeTaskForm.number) {
		ElMessage.error('请输入抽样编号');
		return false;
	}
	// 其他校验逻辑
	return true;
}

const emit = defineEmits(['update:modelValue']);

async function handleSaveConfig() {
	// if (!validateForm()) return;
	console.log(configTable.value);
	const formattedNumber = schemeTaskForm.number.toString().padStart(3, '0'); // 将数字填充成3位，不足的前面补0
	configTable.value = configTable.value.map(item => {
		return {
			workpieceNumber: `#${formattedNumber}`,
			...item
		};
	});
	emit('update:modelValue', {
		number: schemeTaskForm.number,
		list: configTable.value
	});
}


</script>
<style>
.dialog-footer {
	text-align: right;
}
</style>